package com.ali.framework.controller;

import com.ali.framework.feign.HystrixFeign;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Smile
 * @Date: 2023/12/14/11:33
 * @Description:
 */
@RestController
@DefaultProperties(defaultFallback = "hystrixGlobalFallback")
public class HystrixController {

    @Autowired
    private HystrixFeign hystrixFeign;

    /**
     * 服务降级
     * @return
     */
    @GetMapping("/hystrix")
    @HystrixCommand(fallbackMethod = "hystrixFallback",commandProperties = {
        @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1000")
    })
    public String hystrix(){
        return hystrixFeign.test(1L);
    }

    public String hystrixFallback(){
        return "hystrixFallback";
    }

    @GetMapping("/hystrixs")
    @HystrixCommand //默认走全局fallback
    public String hystrixs(){
        return hystrixFeign.test(1L);
    }

    //全局处理fallback
    public String hystrixGlobalFallback(){
        return "全局处理fallback";
    }
//------------------------------------------------------------------
    /**
     * 服务熔断
     * @return
     */
    @HystrixCommand(fallbackMethod = "hystrixFallback",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enable",value = "true"),//是否开启断路器
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),//请求次数，在时间窗口内达到多少10次请求后，才进行判断
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "50"),//失败几次后断开
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),//时间窗口
            /*@HystrixProperty(name = "metrics.rollingStats.numBuckets",value = "10"),*/
    })
    @GetMapping("/hystrix2")
    public String hystrix2(){
        return hystrixFeign.test(1L);
    }


}
